<?php
require_once('Zend/Auth/Adapter/Interface.php');
require_once('application/models/Users.php');

class ADODBAuthInterface implements Zend_Auth_Adapter_Interface
{
	private $db;
	private $tableName;
	private $nameColumn;
	private $passColumn;
	private $statusesColumn;

	private $name;
	private $password;
	private $statusesId;

	public function __construct($db)
	{
		$this->db = $db;
		$this->tableName = 'users';
		$this->nameColumn = 'username';
		$this->passColumn = 'password';
		$this->statusesColumn = 'statuses_id';
	}

	public function setTableName($table)
	{
		$this->tableName = $table;
		return $this;
	}

	public function setIdentityColumn($nameCol)
	{
		$this->nameColumn = $nameCol;
		return $this;
	}

	public function setCredentialColumn($passCol)
	{
		$this->passColumn = $passCol;
		return $this;
	}
	
	public function setStatusesColumn($statusesCol)
	{
		$this->statusesColumn = $statusesCol;
		return $this;
	}
	
	public function setIdentity($name)
	{
		$this->name = $name;
		return $this;
	}

	public function setCredential($pass)
	{
		$this->password = $pass;
		return $this;
	}

	public function setStatusesId($statusesId)
	{
		$this->statusesId = $statusesId;
		return $this;
	}
	
	public function authenticate()
	{
		//Database::instance()->getConn()->debug =1;
		$stmt = $this->db->prepare('
    	SELECT 
    		u.id,
    		u.apps_id,
    		u.statuses_id,
			u.username,
			u.email,
			u.password,
			u.last_name,
			u.first_name,
			u.registration_date,
			u.last_update_date,
			u.is_administrator,
			u.is_operator,
			u.system_roles_id,
			saddr_first_name,
			saddr_last_name,
			saddr_email,
			saddr_street,
			saddr_building_no,
			saddr_apartment_no,
			saddr_postcode,
			saddr_city,
			saddr_country,
			baddr_company_name,
			baddr_first_name,
			baddr_last_name,
			baddr_email,
			baddr_street,
			baddr_building_no,
			baddr_apartment_no,
			baddr_postcode,
			baddr_city,
			baddr_country,
			baddr_vat_id,
			remarks,
			main_image_id
		FROM v_'.$this->tableName.' u
		WHERE u.'.$this->nameColumn.' = ? AND u.'.$this->passColumn.' = ? AND u.'.$this->statusesColumn.' = ?');
		
		//print_r($stmt);
		try {
			$result = $this->db->doStatement($stmt,array($this->name,$this->password, $this->statusesId));
		} catch (DatabaseException $e){
			$e->getMessage();
		}
		$authResult = array(
			'code' => Zend_Auth_Result::FAILURE,
			'identity' => $this->name,
			'messages' => array()
		);


		if($result->_numOfRows == 0)
		{
			$authResult['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
			$authResult['messages'][] = 'Login failed';
		}
		else if($result->_numOfRows > 1)
		{
			$authResult['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
			$authResult['messages'][] = 'Login failed';
		}
		else
		{
			$authResult['code'] = Zend_Auth_Result::SUCCESS;
			$authResult['messages'][] = 'Login succesful';

			$user = ModelMapper::instance('Users')->loadArray($result->FetchRow());
			
			
			$auth    = Zend_Auth::getInstance();
			$storage = $auth->getStorage();
			$storage->write(serialize($user));
						
			echo json_encode(array('redirect' => 1));
        	exit;
		}
		//exit();
		return new Zend_Auth_Result($authResult['code'], $authResult['identity'], $authResult['messages']);
	}
}
?>
